/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import Modelo.Investigacion;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import Acceso.Security;

/**
 *
 * @author glpc
 */
public class modificarInvestigacionAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador","Gestionar Investigaciones"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        modificarInvestigacionForm formBean = (modificarInvestigacionForm) form;

        String id_investigacion = formBean.getId_investigacion();
        String fecha_investigacion = formBean.getFecha_investigacion();
        String motivo = formBean.getMotivo();
        String descripcion = formBean.getDescripcion();
        String id_novedad_esp = formBean.getId_novedad_esp();
        String espacio = formBean.getEspacio();
        String sede = formBean.getSede();


        ActionErrors errors = new ActionErrors();
        boolean error = false;

        if (id_investigacion == null || id_investigacion.equals("")) {
            errors.add("inv_id_investigacion", new ActionMessage("errors.inv_id_investigacion"));
            saveErrors(request, errors);
            error = true;
        }
        if (fecha_investigacion == null || fecha_investigacion.equals("")) {
            errors.add("inv_fecha_requerida", new ActionMessage("errors.inv_fecha_requerida"));
            saveErrors(request, errors);
            error = true;
        }
        if (motivo == null || motivo.equals("")) {
            errors.add("inv_motivo_requerido", new ActionMessage("errors.inv_motivo_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (espacio == null || espacio.equals("")) {
            errors.add("inv_espacio_requerido", new ActionMessage("errors.inv_espacio_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (sede == null || sede.equals("")) {
            errors.add("inv_sede_requerida", new ActionMessage("errors.inv_sede_requerida"));
            saveErrors(request, errors);
            error = true;
        }
        if (descripcion == null || descripcion.equals("")) {
            errors.add("inv_descripcion_requerida", new ActionMessage("errors.inv_descripcion_requerida"));
            saveErrors(request, errors);
            error = true;
        }
        if (id_novedad_esp == null || id_novedad_esp.equals("")) {
            errors.add("inv_nov_esp_requerida", new ActionMessage("errors.inv_nov_esp_requerida"));
            saveErrors(request, errors);
            error = true;
        }

         if (error) {
            return mapping.findForward(FAILURE);
        }
        
        Investigacion inv = new Investigacion(id_investigacion, fecha_investigacion, id_novedad_esp, espacio, sede);
        inv.setMotivo(motivo);
        inv.setDescripcion(descripcion);

        if (Modelo.DAL.Investigacion_DAO.actualizarInvestigacion(inv)) {
            return mapping.findForward(SUCCESS);
        } else {
            formBean.setError("Ocurrió un error al modificar la investigación.");
            formBean.setId_investigacion(id_investigacion);
            return mapping.findForward(FAILURE);
        }
    }
}
